<?php
namespace App\Http\Controllers;
use App\AdminUser;
use App\Business;
use App\Customer;
use App\Product;
use App\Contract;
use Psy\Util\Json;
use Illuminate\Support\Facades\DB;

class ContractController extends Controller{
    /**
     * 展示通讯录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function setting(Contract $contract){
        return view('admin.test.contract',compact('contract'));
    }
    public function index()
    {
        return view('admin.contract.index');
    }
    //批量删除
    public function del(){
        $sql="delete from contracts where";
        $column="";
        for($i=0;$i<count($_REQUEST);$i++)
        {
            if($i==(count($_REQUEST)-1)){
                $column=$column."id=".$_REQUEST[$i];
            }else{
                $column=$column."id=".$_REQUEST[$i]." "."or"." ";
            }
        }
        $sql=$sql." ".$column;
        DB::select($sql);
        return [
            "code" => 200,
            'msg' => '删除成功',
        ];
        // $this->DeleteAll( $_REQUEST); //另类删除手法
    }
    /**
     * 数据接口
     */
    public function data()
    {
        $count=Contract::count();
        if ($count>0) {
            if(isset($_GET['key']) && $_GET['key']['key']!="") {
                $key= $_GET['key']['key'];//搜索序列号
                $value= $_GET['key']['value'];//搜索序列号
                if($key!="ID")
                {
                    if($key=="money")
                    {
                        $users = Contract::where($key,'>',$value)->get();
                    }else{
                    $users = Contract::where($key,'like','%'.$value.'%')->get();
                    }
                }
                else {

                    $users = Contract::where([$key=>$value])->get();
                }
            }
            else{
                $page = $_GET['page']; //当前页数
                $limit = $_GET['limit']; //每页的数量
                $page_count = ($page - 1) * $limit;
                $users = Contract::offset($page_count)->limit($limit)->get();
                //$users[0]['user_id']=$users[0]['type']['name'];
            }
            return json_encode(
                [
                    "code" => 0,
                    'msg' => '',
                    'count' => $count,
                    'data' => $users
                ]
            );
        } else{
            return [
                'code'=>0,
                "msg"=>"",
                "count"=>0,
                "data"=>[]
            ];
        }
    }
    /**
     * 添加界面
     */
    public function create(){
        $users=AdminUser::get();
        $customers=Customer::get();
        $business=Business::get();
        //dd($customers);
        return view('admin.contract.create',compact('users','business','customers'));
    }

    /**
     * 添加操作逻辑简单实现
     */
    public function store(){
        $data=array();
        $data['name']=$_REQUEST['name'];
        $data['user_id']=$_REQUEST['user_id'];
        $data['customer_id']=$_REQUEST['customer_id'];
        $data['business_id']=$_REQUEST['business_id'];
        $data['money']=$_REQUEST['money'];
        $start=explode('到',$_REQUEST['datetime'])[0];
        $end=explode('到',$_REQUEST['datetime'])[1];
        $start=rtrim($start);
        $end=ltrim($end);
        $data['start_time']=$start;
        $data['end_time']= $end;
        Contract::create($data);
        return redirect("/admin/contract");
    }
    /*
     * 删除
     */
    public function delete($id)
    {
        Contract::where('id', '=', $id)->delete();
        return back();
    }
    //修改界面
    public function edit(Contract $contract){
        $users=AdminUser::get();
        $customers=Customer::get();
        $business=Business::get();
        //dd($customers);

        return view('admin.contract.update',compact('users','customers','business','contract'));
    }
    public function view_update(Contract $contract){

//        $this->validate(request(), [
//            'title'=>'required|string|max:100|min:5',
//            'content'=>'required|string|min:5'
//        ]);

        //$this->authorize('update','$business');
        $contract->name=request('name'); //创建字段
        $contract->customer_id=request('customer_id'); //创建字段
        $contract->user_id=request('user_id'); //创建字段
        $contract->business_id=request('business_id'); //创建字段
        $contract->money=request('money'); //创建字段
        $start=explode('到',$_REQUEST['datetime'])[0];
        $end=explode('到',$_REQUEST['datetime'])[1];
        $start=rtrim($start);
        $end=ltrim($end);
        $contract->start_time=$start;
        $contract->end_time=$end;
        //$business->remark=request('remark');
        $contract->save();
        return "<script> window.parent.location.reload();</script>";
    }
    //修改逻辑
    public function update(){
      $filds=$this->array_hebing($_REQUEST,'name','customer_id','user_id','business_id','money','status');//传入需要更新的字段
      if(empty($filds))  //判断数据是否为空 是则不修改
      {
          return ['statu'=>'0'];
      }
      else{
          Contract::where('id', $_REQUEST['id'])->update($filds);
          return ['statu'=>'200'];
      }

    }
    //批量删除逻辑
    //原理则是前端发送批量删除的ID组成的数组 通过后端逻辑进行处理

    /**
     * //组成一个我所要的数组用于数据库更新 并且
     * @param array ...$numbers 传进去的字段
     * @return array /￥/返回一个键=字段 值=数值的数组
     */
    public function array_hebing($Data,...$numbers){
      //  print_r($Data);die;

        $data=Contract::where('id', $Data['id'])->first();
        //print_r($_REQUEST);die;
        $filds=[];
        if($Data && is_array($Data)){   //组成一个我所要的数组用于数据库更新
            //print_r($_REQUEST);die;
            foreach($numbers as $fild){
                foreach ($Data as $key=> $value){
                    if($fild==$key)
                    {
                            $filds[$fild] = $value;
                            continue;
                    }
                }
            }
            $start=0;  //循环记数
            foreach($filds as $key=>$value)  //删除请求更新字段中没有修改的字段
            {
                if($data[$key]==$filds[$key]) {   //判断是否相同 相同则从需要更新的数组中删除该键值
                        array_splice($filds, $start, 1);
                        $start=$start-1;
                }
                $start=$start+1;
            }
        }
        return $filds;
    }
}